#!/usr/bin/env python3

import os
import yaml
import utils

def configure():
    # load config files
    with open('defaults.yaml', 'r') as f:
        defaults = yaml.safe_load(f)
    with open('config.yaml', 'r') as f:
        config = yaml.safe_load(f)

    # make directories
    if not os.path.isdir('./lilypond'):
        os.makedirs('./lilypond')

    settings = utils.merge_by_depth(defaults, config)

    # make books
    if 'books' in settings:
        for book in settings['books']:
            if 'instruments' in book:
                book['instruments'] = utils.merge_by_id(settings['instruments'], book['instruments'])
            if 'page-sizes' in book:
                book['page-sizes'] = utils.merge_by_depth(settings['page-sizes'], book['page-sizes'])
            else:
                book['page-sizes'] = settings['page-sizes']
            for page_size in book['page-sizes']:
                with open('lilypond/' + book['id'] + '_' + page_size['id'] + '.ly', 'w') as f:
                    f.write(utils.make_book(book, page_size, settings))
